<template>
  <div class="app-container">
      <el-form label-width="120px">
        <el-form-item label="讲师名称">
          <el-input v-model="Teacher.name"/>
        </el-form-item>
        <el-form-item label="讲师排序">
          <el-input-number v-model="Teacher.sort" controls-position="right" :min="0"/>
        </el-form-item>
        <el-form-item label="讲师头衔">
          <el-select v-model="Teacher.level" clearable placeholder="请选择">
            <el-option :value="1" label="高级讲师"/>
            <el-option :value="2" label="首席讲师"/>
          </el-select>
        </el-form-item>
        <el-form-item label="讲师资历">
          <el-input v-model="Teacher.career"/>
        </el-form-item>
        <el-form-item label="讲师简介">
          <el-input v-model="Teacher.intro" :rows="10" type="textarea"/>
        </el-form-item>

        <!-- 讲师头像：TODO -->
        <el-form-item label="讲师头像">

          <!-- 头衔缩略图 -->
          <pan-thumb :image="String(Teacher.avatar)"/>
          <!-- 文件上传按钮 -->
          <el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">更换头像
          </el-button>
          <image-cropper
            v-show="imagecropperShow"
            :width="300"
            :height="300"
            :key="imagecropperKey"
            :url="BASE_API+'/service-oss/DongBing/Oss/upload'"
            field="multipartFile"
            @close="close"
            @crop-upload-success="cropSuccess"/>
        </el-form-item>

        <el-form-item>
          <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
        </el-form-item>
      </el-form>
  </div>
</template>

<script>

import { saveTeacher,getTeacherById,updateTeacher } from '@/api/Teacher';
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'

export default {
  data() {
    return {
      imagecropperShow:false,
      imagecropperKey:0,
      BASE_API:process.env.BASE_API,
      saveBtnDisabled:false,
      Teacher:{},
    }
  },
  components:{
    ImageCropper,PanThumb
  },
  created() {
    if (this.$route.params && this.$route.params.id) {
      this.getTeacherInfo(this.$route.params.id)
    }
  },
  watch:{
    $route(){
      if (this.$route) {
        this.Teacher = {}
      }
    }
  },
  methods: {
    close(){
      this.imagecropperShow=false
      //上传组件初始化
      this.imagecropperKey = this.imagecropperKey+1
    },
    cropSuccess(data){
      this.imagecropperShow=false
      //上传之后接口返回图片地址
      this.Teacher.avatar = data
      // 刷新页面
      this.imagecropperKey = this.imagecropperKey+1
    },
    saveOrUpdate(){
      if (!this.Teacher.id) {
        this.addTeacher()
      }else {
        this.updateTeacher()
      }
    },
    updateTeacher(){
      updateTeacher(this.Teacher).then(res=>{
        this.$notify({
          title: '操作成功',
          type: 'success',
          message: res.message
        })
        this.saveBtnDisabled = true
        this.$router.push({path:"/teacher/teacherList"})
      })
    },
    addTeacher(){
      saveTeacher(this.Teacher).then(res=>{
        this.$notify({
          title: '操作成功',
          type: 'success',
          message: res.message
        })
        this.saveBtnDisabled = true
        this.$router.push({path:"/teacher/teacherList"})
      })
    },
    getTeacherInfo(id){
      getTeacherById(id).then(res=>{
         this.Teacher = res.data

      })
    }
  },
}
</script>
